How to delete File in c#

44

how to delete all files in a directory c# -

System.IO.DirectoryInfo di = new DirectoryInfo("YourPath");

foreach (FileInfo file in di.GetFiles())
{
    file.Delete(); 
}
foreach (DirectoryInfo dir in di.GetDirectories())
{
    dir.Delete(true); 
}

find and delete files c# -

string rootFolderPath = @"C:\\SomeFolder\\AnotherFolder\\FolderCOntainingThingsToDelete";
string filesToDelete = @"*DeleteMe*.doc";   // Only delete DOC files containing "DeleteMe" in their filenames
string[] fileList = System.IO.Directory.GetFiles(rootFolderPath, filesToDelete);
foreach(string file in fileList)
{
    System.Diagnostics.Debug.WriteLine(file + "will be deleted");
//  System.IO.File.Delete(file);
}

How to delete File in c# -

string gpath;

        string path=@"c:\Users\Adam\Desktop\";

        string name="file";

        string f="";

        int i=0;

        string ext=".txt";

        while(File.Exists(path + name + f + ext))
        {
            i++;

            f = i.ToString();
        }

        gpath = path + name + f + ext;

        button2.Enabled = true;

        File.Create(gpath);

        File.Delete(gpath);//why there is an Error??

Comments

Submit
0 Comments